﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class DTaxiNoPsgDistanceConfig
    {
        public bool Exists(string phoneNumber)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ttaxinopsgdistanceconfig");
            strSql.Append(" where PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.String)
             };
            parameters[0].Value = phoneNumber;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.TaxiNoPsgDistanceConfig model)
        {
            if (!Exists(model.PhoneNumber))
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into ttaxinopsgdistanceconfig(");
                strSql.Append("PhoneNumber,StartTime1,EndTime1,");
                strSql.Append("Distance1,StartTime2,EndTime2,Distance2)");
                strSql.Append(" values (");
                strSql.Append("@PhoneNumber,@StartTime1,@EndTime1,");
                strSql.Append("@Distance1,@StartTime2,@EndTime2,@Distance2)");
                MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,12),
					new MySqlParameter("@StartTime1", MySqlDbType.DateTime),
                    new MySqlParameter("@EndTime1", MySqlDbType.DateTime),                  
                    new MySqlParameter("@Distance1", MySqlDbType.Int32),
                    new MySqlParameter("@StartTime2", MySqlDbType.DateTime),
                    new MySqlParameter("@EndTime2", MySqlDbType.DateTime),
                    new MySqlParameter("@Distance2", MySqlDbType.Int32)};

                parameters[0].Value = model.PhoneNumber;
                parameters[1].Value = model.StartTime1;
                parameters[2].Value = model.EndTime1;
                parameters[3].Value = model.Distance1;
                parameters[4].Value = model.StartTime2;
                parameters[5].Value = model.EndTime2;
                parameters[6].Value = model.Distance2;
                int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
                if (rows > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return Update(model);
            }
        }

        public Model.TaxiNoPsgDistanceConfig GetModel(string  phoneNumber)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from ttaxinopsgdistanceconfig ");
            strSql.Append(" where PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,12)
			};
            parameters[0].Value = phoneNumber;

            Model.TaxiNoPsgDistanceConfig model = new Model.TaxiNoPsgDistanceConfig();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StartTime1"] != null && ds.Tables[0].Rows[0]["StartTime1"].ToString() != "")
                {
                    model.StartTime1 = DateTime.Parse(ds.Tables[0].Rows[0]["StartTime1"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EndTime1"] != null && ds.Tables[0].Rows[0]["EndTime1"].ToString() != "")
                {
                    model.EndTime1 = DateTime.Parse(ds.Tables[0].Rows[0]["EndTime1"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Distance1"] != null && ds.Tables[0].Rows[0]["Distance1"].ToString() != "")
                {
                    model.Distance1 = int.Parse(ds.Tables[0].Rows[0]["Distance1"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StartTime2"] != null && ds.Tables[0].Rows[0]["StartTime2"].ToString() != "")
                {
                    model.StartTime2 = DateTime.Parse(ds.Tables[0].Rows[0]["StartTime2"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EndTime2"] != null && ds.Tables[0].Rows[0]["EndTime2"].ToString() != "")
                {
                    model.EndTime2 = DateTime.Parse(ds.Tables[0].Rows[0]["EndTime2"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Distance2"] != null && ds.Tables[0].Rows[0]["Distance2"].ToString() != "")
                {
                    model.Distance2 = int.Parse(ds.Tables[0].Rows[0]["Distance2"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
         
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.TaxiNoPsgDistanceConfig model)
        {

            //strSql.Append("@PhoneNumber,@StartTime1,@EndTime1,");
            //strSql.Append("@Distance1,@StartTime2,@EndTime2,@Distance2)");
             
            //,StartLat,StartLng,");
            //strSql.Append("EndLat,EndLng,Distance,State)");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ttaxinopsgdistanceconfig set ");            
            strSql.Append("StartTime1=@StartTime1,");
            strSql.Append("EndTime1=@EndTime1,");
            strSql.Append("Distance1=@Distance1,");

            strSql.Append("StartTime2=@StartTime2,");
            strSql.Append("EndTime2=@EndTime2,");
            strSql.Append("Distance2=@Distance2");

            strSql.Append(" where PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {
					new MySqlParameter("@StartTime1", MySqlDbType.DateTime),
					new MySqlParameter("@EndTime1", MySqlDbType.DateTime),
                    new MySqlParameter("@Distance1", MySqlDbType.Int32),  
                    new MySqlParameter("@StartTime2", MySqlDbType.DateTime),
                    new MySqlParameter("@EndTime2", MySqlDbType.DateTime),
                    new MySqlParameter("@Distance2", MySqlDbType.Int32),
                    new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,12)};
            parameters[0].Value = model.StartTime1;
            parameters[1].Value = model.EndTime1;
            parameters[2].Value = model.Distance1;
            parameters[3].Value = model.StartTime2;
            parameters[4].Value = model.EndTime2;
            parameters[5].Value = model.Distance2;
            parameters[6].Value = model.PhoneNumber;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ttaxinopsgdistanceconfig ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
      
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM ttaxinopsgdistanceconfig ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        
    }
}
